package com.v.air.app.http;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.v.air.app.util.ToastUtil;

public class HttpMethod {
	//PM2.5KEY     test: 5j1znBVAsnSf5xQyNQyq
	//appkey:  GRRN3qdasPj8wZpjrn1i
	private String token = "GRRN3qdasPj8wZpjrn1i";
	public final String TOKEN = "token";
	public final String CITY = "city";

	// 获取一个城市所有监测点的PM2.5数据
	public static final String PM_2_5_URL = "http://www.pm25.in/api/querys/pm2_5.json";
	// 获取一个城市所有监测点的PM10数据
	public static  final String PM_10_URL = "http://www.pm25.in/api/querys/pm10.json";
	// 获取一个城市的监测点列表
	public static  final String PM_station_URL = "http://www.pm25.in/api/querys/station_names.json";

	private static HttpMethod httpMethod;

	public static HttpMethod getInstance() {
		if (httpMethod == null) {
			synchronized (HttpMethod.class) {
				if (httpMethod == null) {
					httpMethod = new HttpMethod();
				}
			}
		}
		return httpMethod;
	}

	public void getData(Context context, String url, String city,
			final HttpResultInterface httpResult) {
		if (HttpTool.checkNetwork(context)) {
			Log.i("URL", url);
			StringBuilder builder = new StringBuilder(url);
			builder.append("?" + TOKEN + "=" + token);
			builder.append("&" + CITY + "=" + city);
			HttpTool.get(context, builder.toString().trim(),
					new StringHandler() {

						@Override
						public void success(String response) {
							httpResult.onHttpSuccess(response);
						}

						@Override
						public void failure(int statusCode,
								String responseBody, Throwable e) {
							httpResult.onHttpError(statusCode, responseBody, e);
						}
					});
		} else {
			ToastUtil.showToast(context, "网络连接失败!", Toast.LENGTH_SHORT);
		}
	}
}
